<template>
       <view class="page_content">
       	<view class="menu" style="margin-top: 30px;">
			<view class="item" @click="upload('one')">
				<view class="img_view" >
					<image :src="one.src" class="image"></image>
				</view>
				<text class="txt">{{one.name}}</text>
			</view>
			<view class="item" @click="upload('two')">
				<view class="img_view" >
					<image :src="two.src" class="image"></image>
				</view>
				<text class="txt">{{two.name}}</text>
			</view>  
		</view>
		<view class="" style="margin-top: 30px;">
			<text class="txt">当前位置:{{address}}</text>
		</view> 
		<view class="btn-row" v-if="one.user_id!=0 && two.user_id!=0 && one.user_id!=two.user_id">
			<button type="primary" class="primary" @tap="testing">开始检测</button>
		</view>
		<view class="btn-row" v-if="one.user_id!=0 && one.user_id==two.user_id">
			<button type="button" class="" >两位检测人员不能一样</button>
		</view>
		</view>
</template>

<script>
	import api from '../../api.js';
	var util = require('../../common/util.js');
    export default {
        data() {
            return {
				id:0,
                one:{
					user_id:0,
					src:'/static/profile.png',
					name:'检测人1'
				},two:{
					user_id:0,
					src:'/static/profile.png',
					name:'检测人2'
				},
				address:'获取中...',
				gps:''
            }
        },
		onLoad: function(e) {
			this.id=e.id;
			this.getLocation();
		},methods: {
			testing(){
				let _this = this;
				api.post({
					url: 'api/updateGPS.json',
					data: {
						id:_this.id ,
						gps:_this.gps ,
						tAddress:_this.address,
						t1Id:_this.one.user_id,
						t2Id:_this.two.user_id
					},
					success: (data) => {
					},
					fail() {
						console.log("失败ing....")
					}
				})
				uni.navigateTo({
					url: '/pages/jiance/test?id='+this.id+"&url="+encodeURIComponent(api.HOST+"/api/test.do?id="+this.id)
				})
			},
			getLocation() {
				let _this = this;
				plus.geolocation.getCurrentPosition(function(p){
					//console.log(JSON.stringify(p));
					_this.gps = p.coords.longitude + "," + p.coords.latitude;
					_this.address = p.addresses.substr(p.addresses.indexOf('市')+1)
				}, function(e){
					_this.address = '获取失败，请打开GPS 和 授权APP获取定位权限'
					uni.showModal({
						title:"位置获取失败",
						showCancel:false,
						content:JSON.stringify(e)
					})
				} );
			},
			upload(mid){
				let _this = this;
				if(this.gps==''){
					return uni.showToast({
						title:"请先获取位置！"
					})
				}
				util.imgHandle.chooseImage(1, ['original', 'compressed'], ['camera']).then(function(res) {
					//加载
					uni.showLoading({
						title:"上传中...",
						mask:true
					})
					api.uploadFile({
						url: 'core/file/uploadAttachment.json',
						filePath: res,
						formData:{
							mark:'shuiyin',
							address:_this.address,
							bizType:'face'
						},
						name: 'file',
						success: (data) => {
							uni.hideLoading();
							if (data.code == 0) {
								_this[mid].src = api.HOST+'/core/file/download/'+data.data.id;
								_this[mid].name ='识别中...'
								_this.face(mid,data.data.id);
							} else {
								uni.showToast({
									icon: 'none',
									title: '上传失败，请重新上传'
								});
							}
						},
						fail() {
							uni.showToast({
								icon: 'none',
								title: '上传失败，请重新上传'
							});
						}
					})
				})
			},
			face(mid,id){
				let _this = this;
				api.post({
					url: 'api/face.do',
					data: {
						id:id 
					},
					success: (data) => {
						if (data.code == 0) {
							var user=data.data.user_list[0];
							if(user.score>80){
								_this[mid].name =user.user_info;
								_this[mid].user_id =user.user_id;
							}else{
								_this[mid].name ='识别失败，请重试'
							}
						} else {
							_this[mid].name ='识别失败，请重试'
							uni.showModal({
								title: '错误提示',
								content: data.msg,
								showCancel:false
							});
						}
					},
					fail() {
						uni.showToast({
							icon: 'none',
							title: '人脸识别失败'
						});
					}
				})
			}
		}
    }
</script>

<style>
	.page_content {
		width: 100%;
		text-align: center;
	
	}
	.menu {
		margin-left: 10px;
		margin-right: 10px;
		padding-left: 10px;
		padding-right: 10px;
		height: realSize(176px);
		background: rgba(255, 255, 255, 1);
		box-shadow: 0px 10px 10px 0px rgba(0, 161, 124, 0.1);
		border-radius: 10px;
		display: flex;
		flex-direction: row;
		align-items: stretch;
		justify-content: space-between;
	
		
	}
	.item {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		width:50%
	}
	.img_view {
		border-radius: 30px;
		display: flex;
		align-items: center;
		justify-content: center;
	}
		
	.txt {
		margin-top: 5px;
		font-size: 14px;
		color: rgba(51, 51, 51, 1);
	}
.image {
	width: 150px;
	height: 150px;
	margin-top: 20px;
}
</style>